package cn.ffcs.uec.selfstaff.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Tab;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffCostB;
import cn.ffcs.uec.outStaff.model.UecOutStaffCostH;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.model.UecOutStaffWork;
import cn.ffcs.uec.selfstaff.action.bean.UecSelfStaffMainBean;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.UomZkUtil;

@Controller
@Scope("prototype")
public class UecSelfStaffMainComposer extends BasePortletComposer implements
		IPortletInfoProvider {

	private static final long serialVersionUID = -5929030282722775845L;

	private UecSelfStaffMainBean bean = new UecSelfStaffMainBean();

	private UecOutStaffInfo uecOutStaffInfo;
	
	private UecOutStaffWork uecOutStaffWork;
	
	private UecOutStaffCostB uecOutStaffCostB;
	
	private UecOutStaffCostH uecOutStaffCostH;
	
	private UecOutStaffManager uecOutStaffManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");
	
	@Override
	public String getPortletId() {
		return super.getPortletId();
	}

	@Override
	public ThemeDisplay getThemeDisplay() {
		return super.getThemeDisplay();
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		UomZkUtil.autoFitHeight(comp);// 使用<borderlayout>时，需要加上这句进行初始化，不然从仓库调出的页面，不能显示
		Components.wireVariables(comp, bean);
		bean.getUecOutStaffInfoListboxExt().setPortletInfoProvider(this);
		bean.getUecOutStaffInfoListboxExt().addForward(
				UecOutStaffConstants.ON_UEC_OUT_STAFF_INFO_QUERY, comp,
				UecOutStaffConstants.ON_UEC_OUT_STAFF_WORK_QUERY_RESPONSE);

		bean.getUecOutStaffInfoListboxExt().addForward(
				UecOutStaffConstants.ON_CLEAN_TAB_UEC_OUT_STAFF_INFO_QUERY, comp,
				UecOutStaffConstants.ON_CLEAN_TAB_UEC_OUT_STAFF_INFO_QUERY_RESPONSE);
		
		bean.getUecOutStaffCostBListboxExt().addForward("onSelectUecOutStaffCostB", comp, "onSelectUecOutStaffCostBRes");
		
		bean.getUecOutStaffCostHListboxExt().addForward("onSelectCostH", comp, "onSelectUecOutStaffCostHRes");
		//外包人员试用情况
		bean.getUecOutStaffTrialListboxExt().addForward(
				UecOutStaffConstants.ON_UEC_OUT_STAFF_TRIAL_QUERY, comp, 
				UecOutStaffConstants.ON_UEC_OUT_STAFF_TRIAL_QUERY_RESPONSE);
		//任职信息
		bean.getUecOutStaffPsnjobListboxExt().addForward(
				UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNJOB_QUERY, comp, 
				UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNJOB_QUERY_RESPONSE);
		//考核信息
		bean.getUecOutStaffAssListboxExt().addForward(
				"onAssesmentQuery", comp, 
				"onAssesmentQueryResponse");
		//政治面貌
		bean.getUecOutStaffPartyLogListboxExt().addForward(
				UecOutStaffConstants.ON_UEC_OUT_STAFF_PARTYLOG_QUERY, comp, 
				UecOutStaffConstants.ON_UEC_OUT_STAFF_PARTYLOG_QUERY_RESPONSE);
		//离退情况
		bean.getUecOutStaffRetireListboxExt().addForward(
				UecOutStaffConstants.ON_UEC_OUT_STAFF_RETIRE_QUERY, comp, 
				UecOutStaffConstants.ON_UEC_OUT_STAFF_RETIRE_QUERY_RESPONSE);
		//组织信息
		bean.getUecOutStaffPsnorgListboxExt().addForward(
				UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNORG_QUERY, comp, 
				UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNORG_QUERY_RESPONSE);
		//人员家庭信息
		bean.getUecOutStaffFamilyListboxExt().addForward(
				UecOutStaffConstants.ON_UEC_OUT_STAFF_FAMILY_QUERY, comp, 
				UecOutStaffConstants.ON_UEC_OUT_STAFF_FAMILY_QUERY_RESPONSE);
		//专业技能
		bean.getUecOutStaffTitleListboxExt().addForward(
				UecOutStaffConstants.ON_UEC_OUT_STAFF_TITLE_QUERY, comp, 
				UecOutStaffConstants.ON_UEC_OUT_STAFF_TITLE_QUERY_RESPONSE);
		//教育培训
		bean.getUecOutStaffTrainListboxExt().addForward(
				UecOutStaffConstants.ON_UEC_OUT_STAFF_TRAIN_QUERY, comp, 
				UecOutStaffConstants.ON_UEC_OUT_STAFF_TRAIN_QUERY_RESPONSE);
		//职业资格
		bean.getUecOutStaffNationdutyListboxExt().addForward(
				UecOutStaffConstants.ON_UEC_OUT_STAFF_NATION_DUTY_QUERY, comp, 
				UecOutStaffConstants.ON_UEC_OUT_STAFF_NATION_DUTY_QUERY_RESPONSE);
	}

	/**
	 * 查询到外包员工基本信息的响应处理.
	 * 
	 * @param event
	 */
	public void onUecOutStaffWorkQueryResponse(final ForwardEvent event) {
		uecOutStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
		if(uecOutStaffInfo != null){
			callTab();
		}
	}
	/**
	 * 查询到外包员费用子表的响应处理.
	 * 
	 * @param event
	 */
	public void onSelectUecOutStaffCostBRes(final ForwardEvent event) {
		uecOutStaffCostB = (UecOutStaffCostB) event.getOrigin().getData();
	}
	/**
	 * 查询到外包员费用主表的响应处理.
	 * 
	 * @param event
	 */
	public void onSelectUecOutStaffCostHRes(final ForwardEvent event) {
		uecOutStaffCostH = (UecOutStaffCostH) event.getOrigin().getData();
	}
	
	public void onClickTab(ForwardEvent event) {
		try {
			Event origin = event.getOrigin();
			if (origin != null) {
				Component comp = origin.getTarget();
				if (comp != null && comp instanceof Tab) {
					bean.setTab((Tab) comp);
					callTab();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * tab响应
	 * 
	 * @throws Exception
	 * 
	 */
	public void callTab() {
		try {
			if (bean.getTab() == null) {
				bean.setTab(bean.getTabBox().getSelectedTab());
			}
			if (uecOutStaffInfo != null) {
				String tab = this.bean.getTab().getId();
				if ("uecOutStaffWork".equals(tab)) {
					Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_WORK_QUERY,
							this.bean.getUecOutStaffWorkListboxExt(), uecOutStaffInfo);
				} else if ("uecOutStaffCostB".equals(tab)) {
					uecOutStaffWork = uecOutStaffManager.findUecOutStaffWork(uecOutStaffInfo);
					Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_COSTB_QUERY,
							this.bean.getUecOutStaffCostBListboxExt(), uecOutStaffWork);
				}else if ("uecOutStaffCostH".equals(tab)) {
					Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_COSTH_QUERY,
							this.bean.getUecOutStaffCostHListboxExt(), uecOutStaffCostB);
				}else if ("uecOutStaffManage".equals(tab)) {
					Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_MANAGE_QUERY,
							this.bean.getUecOutStaffManageListboxExt(), uecOutStaffCostH);
				}else if ("uecOutStaffVocqua".equals(tab)) {
					Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_VOCQUA_QUERY,
							this.bean.getUecOutStaffVocquaListboxExt(), uecOutStaffInfo);
				}else if ("uecOutStaffTramsg".equals(tab)) {
					Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_TRAMSG_QUERY,
							this.bean.getUecOutStaffTramsgListboxExt(), uecOutStaffInfo);
				}else if ("uecOutStaffContract".equals(tab)) {
					Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_CONTRACT_QUERY,
							this.bean.getUecOutStaffContractListboxExt(), uecOutStaffInfo);
				}else if("uecOutStaffDocEdu".equals(tab)) {
					Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_DOC_EDU_QUERY,
							this.bean.getUecOutDocEduListboxExt(),uecOutStaffInfo);
				}else if ("uecOutStaffTrial".equals(tab)) {
					Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_TRIAL_QUERY,
							this.bean.getUecOutStaffTrialListboxExt(), uecOutStaffInfo);
				}else if("uecOutStaffEnc".equals(tab)){
					Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_ENC_QUERY,
						     this.bean.getUecOutStaffEncListboxExt(), uecOutStaffInfo);
				}else if ("uecOutStaffPsnjob".equals(tab)) {
			        Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNJOB_QUERY,
					     this.bean.getUecOutStaffPsnjobListboxExt(), uecOutStaffInfo);
			    }else if ("uecOutStaffAss".equals(tab)) {
			        Events.postEvent("onAssesmentQuery",
					     this.bean.getUecOutStaffAssListboxExt(), uecOutStaffInfo);
			    }else if ("uecOutStaffPartyLog".equals(tab)) {
			        Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_PARTYLOG_QUERY,
					     this.bean.getUecOutStaffPartyLogListboxExt(), uecOutStaffInfo);
			    }else if ("uecOutStaffRetire".equals(tab)) {
			        Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_RETIRE_QUERY,
					     this.bean.getUecOutStaffRetireListboxExt(), uecOutStaffInfo);
			    }else if ("uecOutStaffPsnorg".equals(tab)) {
			        Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNORG_QUERY,
					     this.bean.getUecOutStaffPsnorgListboxExt(), uecOutStaffInfo);
			    }else if ("uecOutStaffFamily".equals(tab)) {
			        Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_FAMILY_QUERY,
					     this.bean.getUecOutStaffFamilyListboxExt(), uecOutStaffInfo);
			    }else if ("uecOutStaffNationduty".equals(tab)) {
			        Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_NATION_DUTY_QUERY,
					     this.bean.getUecOutStaffNationdutyListboxExt(), uecOutStaffInfo);
			    }else if ("uecOutStaffTitle".equals(tab)) {
			        Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_TITLE_QUERY,
					     this.bean.getUecOutStaffTitleListboxExt(), uecOutStaffInfo);
			    }else if ("uecOutStaffTrain".equals(tab)) {
			        Events.postEvent(UecOutStaffConstants.ON_UEC_OUT_STAFF_TRAIN_QUERY,
					     this.bean.getUecOutStaffTrainListboxExt(), uecOutStaffInfo);
			    }else if ("uecOutStaffHiPsnWork".equals(tab)) {
                    Events.postEvent(UecOutStaffConstants.ON_UEC_OUTSTAFF_HI_PSNWORK_QUERY,
                            this.bean.getUecOutStaffHiPsnWorkListboxExt(), uecOutStaffInfo);
                }
			} else {
				// 清空数据
				bean.getUecOutStaffWorkListboxExt().onCleanUecOutWorkResponse(null);
				bean.getUecOutStaffCostBListboxExt().onCleanUecOutCostBResponse(null);
				bean.getUecOutStaffCostHListboxExt().onCleanUecOutCostHResponse(null);
				bean.getUecOutStaffVocquaListboxExt().onCleanUecOutVocquaResponse(null);
				bean.getUecOutStaffTramsgListboxExt().onCleanUecOutTramsgResponse(null);
				bean.getUecOutStaffContractListboxExt().onCleanUecOutContractResponse(null);
				bean.getUecOutDocEduListboxExt().onCleanUecOutDocEduResponse(null);
				bean.getUecOutStaffTrialListboxExt().onCleanUecOutStaffTrialResponse(null);
				bean.getUecOutStaffEncListboxExt().onCleanUecOutStaffEncResponse(null);
				bean.getUecOutStaffPsnjobListboxExt().onUecOutStaffPsnjobQueryResponse(null);
				bean.getUecOutStaffAssListboxExt().onUecOutStaffAssQueryResponse(null);
				bean.getUecOutStaffPartyLogListboxExt().onUecOutStaffPartyLogQueryResponse(null);
				bean.getUecOutStaffPsnorgListboxExt().onUecOutStaffPsnorgQueryResponse(null);
				bean.getUecOutStaffRetireListboxExt().onUecOutStaffRetireQueryResponse(null);
				bean.getUecOutStaffFamilyListboxExt().onUecOutStaffFamilyQueryResponse(null);
				bean.getUecOutStaffNationdutyListboxExt().onUecOutStaffNationdutyQueryResponse(null);
				bean.getUecOutStaffTitleListboxExt().onUecOutStaffTitleQueryResponse(null);
				bean.getUecOutStaffTrainListboxExt().onUecOutStaffTrainQueryResponse(null);
				bean.getUecOutStaffHiPsnWorkListboxExt().onUecOutStaffHiPsnWorkQueryResponse(null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}	

	/**
	 * 处理点击后Tabs .
	 * 
	 * @param event
	 */
	public void onCleanTabUecStaffInfoQueryResponse(final ForwardEvent event) {
		uecOutStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
		if(uecOutStaffInfo == null){
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_WORK,
					bean.getUecOutStaffWorkListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_COSTB,
					bean.getUecOutStaffCostBListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_COSTH,
					bean.getUecOutStaffCostHListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_VOCQUA,
					bean.getUecOutStaffVocquaListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_TRAMSG,
					bean.getUecOutStaffTramsgListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_CONTRACT,
					bean.getUecOutStaffContractListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_TRIAL,
					bean.getUecOutStaffTrialListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_ENC,
					bean.getUecOutStaffEncListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PSNJOB,
					bean.getUecOutStaffPsnjobListboxExt(), null);
			Events.postEvent("onCleanAssesmentQuery",
					bean.getUecOutStaffAssListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PARTYLOG,
					bean.getUecOutStaffPartyLogListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PSNORG,
					bean.getUecOutStaffPsnorgListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_RETIRE,
					bean.getUecOutStaffRetireListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_FAMILY,
					bean.getUecOutStaffFamilyListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_NATION_DUTY,
					bean.getUecOutStaffNationdutyListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_RETIRE,
					bean.getUecOutStaffRetireListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_TITLE,
					bean.getUecOutStaffTitleListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_UEC_OUT_DOC_EDU,
					bean.getUecOutStaffContractListboxExt(), null);
			Events.postEvent(UecOutStaffConstants.ON_CLEAN_OUTSTAFF_HI_PSNWORK,
                    bean.getUecOutStaffHiPsnWorkListboxExt(), null);
			
			callTab();
		}
	}

}
